package com.xc.controller;

import com.aliyun.oss.ClientException;
import com.xc.pojo.Result;
import com.xc.utils.MyALiYunOSSUtilsPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class MyUploadController {

    @Autowired
    private MyALiYunOSSUtilsPlus aLiYunOSSUtilsPlus;

    @PostMapping("/upload")
    public Result localUpload(MultipartFile image) throws IOException, ClientException, com.aliyuncs.exceptions.ClientException {
        // 获取真是的文件名
        String filename = image.getOriginalFilename();
        System.out.println("----------------真是的文件名是:------------------"+filename);

        // 面向 file 对象,直接完成文件复制的行为,为了防止文件名重名覆盖,建议将文件名使用UUID随机的名词替换
        //String s = UUID.randomUUID().toString();
        // 在目的地创建文件对象
        /*File f = new File("D:\\a\\"+s+filename.substring(filename.lastIndexOf(".")));
        file.transferTo(f);*/

        // 阿里云上传
        //String s1 = MyALiYunOSSUtils.uploadFile(image);
        String s1 = aLiYunOSSUtilsPlus.uploadFile(image);
        if(s1 == null){
            return Result.error("上传阿里云失败@");
        }
        return Result.success(s1);

    }

}